<?php

class infoContent extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'infoContent':
	 * @var integer $id
	 * @var integer $create_time
	 * @var integer $update_time
	 * @var string $create_by
	 * @var string $update_by
	 * @var string $title
	 * @var string $brief
	 * @var string $display_pic_path
	 * @var string $content
	 * @var string $source_url
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'infoContent';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('create_by','length','max'=>128),
			array('update_by','length','max'=>128),
			array('title','length','max'=>128),
			array('brief','length','max'=>200),
			array('display_pic_path, source_url','length','max'=>256),
			array('create_time', 'required'),
			array('create_time, update_time, is_hot', 'numerical', 'integerOnly'=>true),
			array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
//		    'related'=>array(self::MANY_MANY, 'infoContent', 'infoRelated(infoId, relatedId)',
//				'together'=>true,
//				'joinType'=>'LEFT JOIN',),
            'tagFilter'=>array(self::MANY_MANY, 'Tag', 'InfoTag(infoId, tagId)',
				'together'=>true,
				'joinType'=>'INNER JOIN',
				'condition'=>'??.name=:tag'),
		);
	}

    /**
	 * @return array tags
	 */
	public function getTagArray()
	{
		return array_unique(preg_split('/\s*,\s*/',trim($this->tags),-1,PREG_SPLIT_NO_EMPTY));
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id'=>'Id',
			'create_time'=>'Create Time',
			'update_time'=>'Update Time',
			'create_by'=>'Create By',
			'update_by'=>'Update By',
			'title'=>'Title',
			'brief'=>'Brief',
			'display_pic_path'=>'Display Pic Path',
		    'source_url'=>'Source URL',
			'content'=>'Content',
		    'is_hot'=>'Is Hot',
		    'tags'=>'Tags',
		);
	}
	
    public function safeAttributes()
	{
	    return array('title', 'display_pic_path', 'content','brief', 'source_url', 'is_hot','tags');
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		$parser=new CMarkdownParser;
		if($this->isNewRecord)
		{
			$this->create_time=$this->update_time=time();
//			$this->authorId=Yii::app()->user->username;
		}
		else
			$this->update_time=time();
		return true;
	}
	
	/**
	 * Postprocessing after the record is saved
	 */
	protected function afterSave()
	{
		if(!$this->isNewRecord)
			$this->dbConnection->createCommand('DELETE FROM InfoTag WHERE infoId='.$this->id)->execute();

	    foreach($this->getTagArray() as $name)
		{
			if(($tag=Tag::model()->findByAttributes(array('name'=>$name)))===null)
			{
				$tag=new Tag(array('name'=>$name));
				$tag->save();
			}
			$this->dbConnection->createCommand("INSERT INTO InfoTag (infoId, tagId) VALUES ({$this->id},{$tag->id})")->execute();
		}
	}
	
    /**
	 * Postprocessing after the record is deleted
	 */
	protected function afterDelete()
	{
		// The following two deletions are mainly for SQLite database.
		// In other DBMS, the related row deletion is enforced by FK constraints
		$this->dbConnection->createCommand('DELETE FROM InfoTag WHERE infoId='.$this->id)->execute();
	}
}